﻿using BLL;
using DAL;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web
{
    public partial class Goodsadd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GoodsType();
            }
        }
        void GoodsType()
        {
            this.DropDownList1.DataSource = UsersDAL.GoodsType();
            this.DropDownList1.DataTextField = "GoodsTypeName";
            this.DropDownList1.DataValueField = "GoodsTypeID";
            DataBind();
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextBox8.Text = "";
            TextBox9.Text = "";
            Image1.ImageUrl = "";
            Label7.Text = "请上传图片!";
           
            DropDownList1.SelectedIndex = 0;
         
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if ((TextBox1.Text != ""  && TextBox2.Text != "" && TextBox3.Text != "" && TextBox4.Text != "" && Image1.ImageUrl !="")&&(TextBox5.Text!=""&&TextBox6.Text!=""&&TextBox7.Text!=""))
            {
                if (UsersDAL.GoodsName(TextBox1.Text).Rows.Count > 0)
                {
                    Response.Write("<script>alert('商品有重复的请重新输入!')</script>");
                    Button2_Click(1, e);
                }
                else
                {

                    Models.Goods gs = new Models.Goods();
                    gs.GoodsName = TextBox1.Text.Trim();
                    gs.GoodsTypeID = int.Parse(DropDownList1.SelectedValue);
                    gs.GoodsPrice = Convert.ToDecimal(TextBox3.Text);
                    gs.GoodsDesc = TextBox4.Text.Trim();
                    gs.GoodsNum = int.Parse(TextBox2.Text.Trim());
                    gs.GoodsPic = Image1.ImageUrl;

                    
                    if (UsersBLL.GoodsAdd(gs))
                    {

                        Models.GoodsDesc goodsDesc = new Models.GoodsDesc();
                        goodsDesc.GoodsID = UsersDAL.GoodID();
                        goodsDesc.GoodsDescPic1 = TextBox5.Text.Trim();
                        goodsDesc.GoodsDescPic2 = TextBox6.Text.Trim();
                        goodsDesc.GoodsDescPic3 = TextBox7.Text.Trim();
                        goodsDesc.GoodsDescPic4 = TextBox8.Text.Trim();
                        goodsDesc.GoodsDescPic5 = TextBox9.Text.Trim();
                        if (UsersBLL.GoodsDEsc(goodsDesc))
                        {
                            Response.Write("<script>alert('添加成功!')</script>");
                        }
                        else
                        {
                            Response.Write("<script>alert('添加失败!')</script>");
                        }
                        Button2_Click(1, e);
                    }
                    else
                    {
                        Response.Write("<script>alert('添加失败!')</script>");
                        Button2_Click(1, e);
                    }
                }
               
            }
            else
            {

                Response.Write("<script>alert('添加的字段不能为空,添加的商品简介照片至少为张！')</script>");
                Button2_Click(1, e);
            }
              
               
            }

        protected void Button3_Click(object sender, EventArgs e)
        {
          
                String fileName = FileUpload1.FileName;
                FileUpload1.SaveAs(Server.MapPath("~/Content/images/Goods/") + fileName);
                Image1.ImageUrl = fileName;
                Label7.Text = "上传成功!";
           
      
        }
    }
    }
